package cn.acewill.yxd.yunxundian.ui.sign.Presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.model.CollegueSchedule;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaLoadListener;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaluationView;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.OEvaLoadModel;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.OEvaPresenter;

/**
 * Created by xxx on 2018/1/12.
 */

public class OEvaLoadPresenterIml implements OEvaPresenter,MEvaLoadListener{

    private MEvaluationView loadView;
    private OEvaLoadModel loadModel;
   public OEvaLoadPresenterIml(MEvaluationView loadView){
       this.loadView = loadView;
       this.loadModel = new OEvaLoadModelIml();
    }
    //加载职能用户
    @Override
    public void requestData(int empid, String year, String month) {
       loadView.showLoadingView();
       loadModel.loadOtherSchedule(empid,year,month,this);
    }

    //加载同事日程
    @Override
    public void requestShopData(int empid, String year, String month) {
        loadView.showLoadingView();
        loadModel.loadOtherShopInfo(empid, year, month, new MEvaLoadListener() {
            @Override
            public void onRequestSuccess(String result) {
                    String[] array = result.split("\\|");
                    if (array[0].equals("200")){
                        if (array[3].equals("noJson")){
                            loadView.hideLoadingView();
                            loadView.showNoData();
                            return;
                        }
                        Gson gson = new Gson();
                        Type typeList = new TypeToken<ArrayList<CollegueSchedule>>() {
                        }.getType();
                        ArrayList<CollegueSchedule> scs = gson.fromJson(pubClass.convertToArrayJson(result), typeList);
                        if (scs.size()>0){
                            loadView.loadCollegSch(scs);
                            loadView.hideLoadingView();
                        }else{
                            loadView.hideLoadingView();
                            loadView.showNoData();
                        }

                    }else{
                        loadView.hideLoadingView();
                        loadView.showNoData();
                    }
            }
            @Override
            public void onRequestEro() {
                loadView.hideLoadingView();
                loadView.showNoData();
            }
        });

    }

    //请求的数据一致
    @Override
    public void onRequestSuccess(String result) {
           String[] array = result.split("\\|");
           if (array[0].equals("200")){

               if (array[3].equals("noJson")){
                   loadView.hideLoadingView();
                   loadView.showNoData();
                   return;
               }
               Gson gson = new Gson();
               Type typeList = new TypeToken<ArrayList<Schedule>>() {
               }.getType();
               ArrayList<Schedule> schedules = gson.fromJson(pubClass.convertToArrayJson(result), typeList);
               if (schedules.size()>0){
                   loadView.updateAdapter(schedules);
                   loadView.hideLoadingView();
               }else{
                   loadView.hideLoadingView();
                   loadView.showNoData();
               }
           }else{
               loadView.hideLoadingView();
               loadView.showNoData();
           }
    }

    @Override
    public void onRequestEro() {
       loadView.hideLoadingView();
       loadView.requestErro();
    }
}
